<template>
  <div>
    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
      <el-form-item label="仓库" prop="storeId">
        <el-select v-model="formData.storeId" placeholder="请选择仓库" clearable :style="{width: '20%'}">
          <el-option v-for="itme in stores" :value=itme.id :label="itme.storeName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label=info prop="busiDate">
        <el-date-picker
          v-model="formData.busiDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注"
                  :style="{width: '20%'}"></el-input>
      </el-form-item>
      <el-button
        size="mini"
        type="text"
        @click="handleGood"
      >添加明细
      </el-button>

      <el-table :data="tableList">
        <el-table-column label="序号" align="center" prop="id"/>
        <el-table-column label="物品" align="center" prop="goodsName"/>
        <el-table-column label="价格" align="center" prop="price">
          <template v-slot="scope">
            <input type="text" v-model="scope.row.price" @change="getTotal(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column label="数量" align="center" prop="count">
          <template v-slot="scope">
            <input type="text" v-model="scope.row.count" @change="getTotal(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column label="小计" align="center" prop="total"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="deleteRow(scope.$index, tableList)"
              type="text"
              size="small">
              移除
            </el-button>
          </template>
        </el-table-column>
      </el-table>


      <el-form-item size="large">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    <el-dialog title="添加" :visible.sync="open" width="%80" append-to-body>
      <el-table :data="goodsList">
        <el-table-column label="序号" align="center" prop="id"/>
        <el-table-column label="物品名称" align="center" prop="goodsName"/>
        <el-table-column label="物品封面" align="center" prop="goodsCover"/>
        <el-table-column label="分类" align="center" prop="categoryId"/>
        <el-table-column label="品牌" align="center" prop="brand"/>
        <el-table-column label="数量" align="center" prop="count"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleSelect(scope.row)"
            >选择此物品
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>
<script>
  import { listStore } from '../../../api/group/store'
  import { listGoods } from '../../../api/group/goods'
  import { addBill } from '../../../api/group/bill'

  export default {
    components: {},
    props: [],
    data() {
      return {
        goodsList:[],
        stores: [],
        queryParams: {},
        open: false,
        tableList: [
        ],
        info: '',
        formData: {
        },
        rules: {
          field101: [{
            required: true,
            message: '请选择下拉选择',
            trigger: 'change'
          }],
          field105: [{
            required: true,
            message: '请选择时间选择',
            trigger: 'change'
          }],
          field106: [{
            required: true,
            message: '请输入多行文本',
            trigger: 'blur'
          }]
        },
        field101Options: [{
          'label': '选项一',
          'value': 1
        }, {
          'label': '选项二',
          'value': 2
        }]
      }
    },
    computed: {},
    watch: {},
    created() {
      if (this.$route.query.info == 1) {
        this.info = '入库时间'
      } else {
        this.info = '出库时间'
      }
      listGoods().then(res => {
        this.goodsList = res.rows
        console.log(this.goodsList)
      })
      listStore().then(res => {
        this.stores = res.rows
      })

    },
    mounted() {
    },
    methods: {
      deleteRow(index, rows) {
        rows.splice(index, 1);
      },
      handleSelect(row) {
        let good = { id: row.id, goodsName: row.goodsName, price: 0, count: 0, total: 0 }
        this.tableList.push(good)
        this.open = false
      },
      handleGood() {
        this.open = true
      },
      getTotal(row) {
        row.total = row.price * row.count
        console.log(row.total)
        console.log(row.price)
        console.log(row.count)
      },
      submitForm() {
        if (this.info=='入库时间'){
          this.formData.type=0
        }else {
          this.formData.type=1
        }
        this.formData.list=this.tableList
        console.log(this.formData)
        addBill(this.formData).then(res=>{
        })
        this.$tab.closeOpenPage({path:"/contract/bill"})
      },
      resetForm() {
        this.$refs['elForm'].resetFields()
      }
    }
  }

</script>
<style>
</style>
